/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.account;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import apus.action.Action;
import apus.action.ActionResult;
import apus.entity.Account;
import apus.persistence.AccountDAO;
import apus.services.ServiceLocator;

/**
 *
 * @author Admin
 */
public class AccountEditAction implements Action {

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            Account account = ServiceLocator.getService(AccountDAO.class).read(id);
            request.setAttribute("account", account);
            return ActionResult.forward("/WEB-INF/View/AccountView/EditAccountView.jsp");
        } else {
            List<Account> accountList = ServiceLocator.getService(AccountDAO.class).findAll();
            request.setAttribute("accountList", accountList);
            return ActionResult.redirect("account.list");
        }
    }

    @Override
    public String getName() {
        return "account.edit";
    }

}